// pages/zhaon_ziown/zhaon_ziown_bianji/zhaon_ziown_bianji.js
import {
  Request
} from "../../../utils/request.js";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: 2070,
    //
    typeindex: '-1',
    //高级信息
    fatherList: [
      {
        name: '一级分类',
        secList: [
          {
            name: '二级分类1',
            thrList: [
              {
                name: '三级分类1',
                mine: '中',
                his: '高',
              },
              {
                name: '三级分类2'
              }
            ]
          },
          {
            name: '二级分类1',
            thrList: [
              {
                name: '三级分类1',
                mine: '中',
                his: '高',
              },
              {
                name: '三级分类2'
              }
            ]
          },
          {
            name: '二级分类1',
            thrList: [
              {
                name: '三级分类1',
                mine: '中',
                his: '高',
              },
              {
                name: '三级分类2'
              },
              {
                name: '三级分类1',
                mine: '中',
                his: '高',
              },
            ]
          },
        ],
      },
      {
        name: '一级分类',
        secList: [
          {
            name: '二级分类1',
            thrList: [
              {
                name: '三级分类1',
                mine: '中',
                his: '高',
              },
              {
                name: '三级分类2'
              }
            ]
          },
        ],
      }
    ],
    //选择后的高级信息
    fatherList_after: [],
    advanced_list: [{
      'title': '德',
      'param': [{
        'title': '心德',
        'param': [{
          'title': '仁爱心',
          'param': [{
            'title': '他的',
            'param': ['中', '高', '优'],
          },
          {
            'title': '想找',
            'param': ['优'],
          }
          ],
        },
        {
          'title': '同理心',
          'param': [{
            'title': '他的',
            'param': ['中', '高', '优'],
          },
          {
            'title': '想找',
            'param': ['优'],
          }
          ],
        },
        ],
      },
      {
        'title': '自德',
        'param': [{
          'title': '仁爱心',
          'param': [{
            'title': '他的',
            'param': ['中', '高', '优'],
          },
          {
            'title': '想找',
            'param': ['优'],
          }
          ],
        },
        {
          'title': '同理心',
          'param': [{
            'title': '他的',
            'param': ['中', '高', '优'],
          },
          {
            'title': '想找',
            'param': ['优'],
          }
          ],
        },
        ],
      },
      {
        'title': '心德',
        'param': [{
          'title': '仁爱心',
          'param': [{
            'title': '他的',
            'param': ['中', '高', '优'],
          },
          {
            'title': '想找',
            'param': ['优'],
          }
          ],
        },
        {
          'title': '同理心',
          'param': [{
            'title': '他的',
            'param': ['中', '高', '优'],
          },
          {
            'title': '想找',
            'param': ['优'],
          }
          ],
        },
        ],
      },
      {
        'title': '自德',
        'param': [{
          'title': '仁爱心',
          'param': [{
            'title': '他的',
            'param': ['中', '高', '优'],
          },
          {
            'title': '想找',
            'param': ['优'],
          }
          ],
        },
        {
          'title': '同理心',
          'param': [{
            'title': '他的',
            'param': ['中', '高', '优'],
          },
          {
            'title': '想找',
            'param': ['优'],
          }
          ],
        },
        ],
      },
      ],
    },],
    basic_list: [],
    open: 1, //切换基本信息、高级信息、印象
    typsstatus: '0', //切换分类显示状态
    firid: '', //一级分类id
  },

  /**
   * 点击显示高级信息(二级)
   */
  showtypes: function (index) {
    console.dir(this.zhaon)
    if (this.zhaon != undefined) {
      this.data.fatherList = this.zhaon;

    }
    console.log(this.data.fatherList)
    // console.log(index.currentTarget.dataset.index)
    //fatherList 数组元素的第一层索引值下标
    let firid = index.currentTarget.dataset.index
    //fatherList对应的id
    let hei_id = this.data.fatherList[index.currentTarget.dataset.index].hei_id
    //fatherList对应id名称
    let hei_name = this.data.fatherList[index.currentTarget.dataset.index].hei_name
    if (this.data.typeindex != index.currentTarget.dataset.index) {
      this.data.typeindex = index.currentTarget.dataset.index
      //调用接口，显示高级信息
      new Request('senior/Get_Senior_List/GetLevel').request(
        () => {
          return {
            openid: app.globalData.openid,
            userid: app.globalData.userid,
            hei_id: this.data.fatherList[index.currentTarget.dataset.index].hei_id,
            level: '1'
          }
        },
        (res) => {
          // console.log(this.data.fatherList)
          if (this.data.fatherList[0] == undefined) {
            this.setData({
              secList: res.data.data
            })
          } else if (this.data.fatherList[index.currentTarget.dataset.index].children == undefined) {
            this.setData({
              secList: res.data.data
            })
          } else {
            this.setData({
              secList: this.data.fatherList[index.currentTarget.dataset.index].children
            })
          }
        }
      );
    } else if (this.data.typeindex == index.currentTarget.dataset.index) {
      this.data.typeindex = null
    }
    this.setData({
      typeindex: this.data.typeindex,
      firid: firid,
      hei_id: hei_id,
      hei_name: hei_name
    });
    //调用之前关闭
    clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
    //开启定时器 铺获
    this._MonitorPage('#rich_two');
  },


  _xuanzhe: function () {
    wx.navigateTo({
      url: '/pages/zhaon_xuanzhe/zhaon_xuanzhe',
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // new Request('Zycolumnlist/getcolumnlist').request(
    //   () => {
    //     return {
    //       openid: app.globalData.openid,
    //       userId: app.globalData.userid
    //     }
    //   },
    //   (res) => {
    //     console.log("执行了请求");
    //     console.log(res);
    //     this.setData({
    //       heightinfo: res.data
    //     });
    //   });

    // new Request('Zycolumnlist/getcolumnopt').request(
    //   () => {
    //     return {
    //       openid: app.globalData.openid,
    //       userId: app.globalData.userid
    //     }
    //   },
    //   (res) => {
    //     // console.log("节点");
    //     // console.log(res);
    //     this.setData({
    //       advanced_list: res.data
    //     })
    //     console.log(res.data);
    //   });

    //获取高级信息列表(标题)
    new Request('senior/Get_Senior_List/GetSeniorInfo').request(
      () => {
        return {
          openid: app.globalData.openid,
          userid: app.globalData.userid,
        }
      },
      (res) => {
        res.data.data.map((e) => {
          // e.children = [];
        });
        this.setData({
          fatherList: res.data.data
        })
      }
    );


    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('account', (data) => {
      console.log(data)
      this.account_info = data;
    });
    let that = this;
    //基本信息标题接口
    new Request('basic/all/Get_All_List/GetList').request(
      () => {
        return {
          openid: app.globalData.openid,
          userId: app.globalData.userid
        }
      },
      (res) => {
        // console.log("请求了新数据");
        console.log(res.data.data)
        res.data.data.map((e) => {
          
          // e.self_id = 0;
          // e.want_id = 0;
          // e.self_text = '未选择';
          // e.want_text = '不限';
          // //默认是单一满足 1是单一满足 2是全部满足
          // e.match_status = 1;
        });
        that.setData({
          basic_list: res.data.data,
        })
      }
    );
  },

  /**
   * 跳转选择页面(单选)
   */
  Choosemine: function (e) {
    console.log(this.data.secList)
    // console.log('单选')
    // console.log(this.data.secList)
    // console.log(this.data.secList[e.currentTarget.dataset.ddid].id)
    // console.log(this.data.secList[e.currentTarget.dataset.ddid].children[e.currentTarget.dataset.id].id)
    // console.log(this.data.secList[e.currentTarget.dataset.ddid].children[e.currentTarget.dataset.id].id)
    if(e.currentTarget.dataset.id != 'end'){
      //选择项id
      var Chooseid = this.data.secList[e.currentTarget.dataset.ddid].children[e.currentTarget.dataset.id].id
    }else{
      //选择项id
      var Chooseid = this.data.secList[e.currentTarget.dataset.ddid].id
    }
    
    //secList 元素数组的第一层下标索引
    let father_index = e.currentTarget.dataset.ddid
    //secList 元素数组的第二层下标索引
    let son_index = e.currentTarget.dataset.id
    //
    let secList = this.data.secList
    //一级分类下标
    let firid = this.data.firid
    //一级分类id
    let hei_id = this.data.hei_id
    //一级分类名称
    let hei_name = this.data.hei_name
    // let own_choose_name = this.data.secList[e.currentTarget.dataset.ddid].children[e.currentTarget.dataset.id].own_choose_name
    // let own_choose_id = this.data.secList[e.currentTarget.dataset.ddid].children[e.currentTarget.dataset.id].own_choose_id


    wx.navigateTo({
      url: '/pages/zhaon_register/lucy_xuanze/lucy_xuanzhe',
      success: function (res) {
        res.eventChannel.emit('basic', {
          data: Chooseid,
          list_type: 1,
          secList: secList,
          father_index: father_index,
          son_index: son_index,
          firid: firid,
          hei_id: hei_id,
          hei_name: hei_name
        })
      },
      events: {
        back: (res) => {
          // console.log(res.arr_data);
          console.log(res)
          // console.log(this.data.fatherList[res.firid])

          this.zhaon = this.data.fatherList;
          this.zhaon[res.firid].children = res.secList;

          this.data.fatherList[res.firid].children = res.secList
          // let fatherList = this.data.fatherList
          // console.log(fatherList)
          this.data.secList[res.index] = res.arr_data;
          // this.data.basic_list[res.index] = res.arr_data;
          // console.log('secList1',this.data.secList);
          // console.log('secList2',this.data.secList);
          // this.data.fatherList[1] = this.data.secList[0];
          this.setData({
            secList: this.data.secList,
            fatherList: this.zhaon
          });
          // console.log(this.data.secList);
          console.log(this.data.fatherList);
        }
      },
    })
    // new Request('senior/update/update/GetSeniorParam').request(
    //   ()=>{
    //     return{
    //       leve_id: this.data.secList[e.currentTarget.dataset.ddid].children[e.currentTarget.dataset.id].id
    //     }
    //   },
    //   (res)=>{
    //     console.log(res)
    //   }
    // )
  },
  /**
   *  跳转选择页面(多选)
   */
  Choosehis: function (e) {
    // console.log('多选')
    // console.log(this.data.secList[e.currentTarget.dataset.ddid].children[e.currentTarget.dataset.id].id)
    if(e.currentTarget.dataset.id != 'end'){
      //选择项id
      var Chooseid = this.data.secList[e.currentTarget.dataset.ddid].children[e.currentTarget.dataset.id].id
    }else{
      //选择项id
      var Chooseid = this.data.secList[e.currentTarget.dataset.ddid].id
    }
    
    //secList 元素数组的第一层下标索引
    let father_index = e.currentTarget.dataset.ddid
    //secList 元素数组的第二层下标索引
    let son_index = e.currentTarget.dataset.id
    //第二层数组
    let secList = this.data.secList
    //一级分类id
    let hei_id = this.data.hei_id
    //一级分类名称
    let hei_name = this.data.hei_name

    //跳转选择页面
    wx.navigateTo({
      url: '/pages/zhaon_register/lucy_xuanze/lucy_xuanzhe',
      success: function (res) {
        res.eventChannel.emit('basic', {
          data: Chooseid,
          list_type: 2,
          secList: secList,
          father_index: father_index,
          son_index: son_index,
          hei_id: hei_id,
          hei_name: hei_name
        })
      },
      events: {
        back: (res) => {
          console.log(res.arr_data);
          this.data.secList[res.index] = res.arr_data;
          // this.data.basic_list[res.index] = res.arr_data;
          // console.log('secList1',this.data.secList);
          // console.log('secList2',this.data.secList);
          // this.data.fatherList[1] = this.data.secList[0];
          this.setData({
            secList: this.data.secList,
          });
          // console.log(this.data.secList);
          // console.log(this.data.fatherList);
        }
      },
    })


  },



  /**
   * 跳转选择页
   */
  xuanze: function (res) {
    // var that = this;
    // console.log(res.target.dataset);
    // var cname = res.target.dataset.cname;
    let columid = res.currentTarget.dataset.columid;
    let sonid = res.currentTarget.dataset.sonid;
    let index_f = res.currentTarget.dataset.index_f;
    let index_s = res.currentTarget.dataset.index_s;
    let allid1 = [index_f, index_s, columid];
    let allid = allid1.toString();
    // var listtype = res.target.dataset.listtype;
    // var opttype = res.target.dataset.opttype;
    console.log(columid);
    console.log(res);
    // wx.navigateTo({
    //   url: '/pages/zhaon_xuanzhe2/zhaon_xuanzhe2?cname=' + cname + '&columid=' + columid + '&listtype=' + listtype + '&opttype=' + opttype,
    // });
    if (sonid == '1') {
      wx.navigateTo({
        url: '/pages/zhaon_ziown/lucy_ziown_gj_danxuan/lucy_ziown_gj_danxuan',
        events: {
          gj_danxuan: function (data) {
            console.log(data)
            new Request('Zhaon_Update_Height/UpdateWanHeight').request(
              () => {
                return {
                  openid: app.globalData.openid, //用户的openid
                  optid: columid,
                  sid: allid,
                  show_name: data.choose_name,
                  chooes_id: data.choose_id,
                }
              },
              (res) => {
                // this.setData({
                //     detailList: res.data.data
                // })
                console.log(res)
              });

          }.bind(this),
        },
        success: function (res) {
          // 通过gj_danxuan向被打开页面传送数据
          res.eventChannel.emit('gj_danxuan', {
            columid: columid
          });
        }
      });
    }

    if (sonid == '0') {
      wx.navigateTo({
        url: '/pages/zhaon_ziown/lucy_ziown_gj_duoxuan/lucy_ziown_gj_duoxuan',
        events: {
          gj_danxuan: function (data) {
            console.log(data)

            new Request('Zhaon_Update_Height/UpdateOwnHeight').request(
              () => {
                return {
                  openid: app.globalData.openid, //用户的openid
                  optid: columid,
                  sid: allid,
                  show_name: data.choose_name,
                  chooes_id: data.choose_id,
                }
              },
              (res) => {
                // this.setData({
                //     detailList: res.data.data
                // })
                console.log(res)
              });

          }.bind(this),
        },
        success: function (res) {
          // 通过gj_danxuan向被打开页面传送数据
          res.eventChannel.emit('gj_danxuan', {
            columid: columid
          });
        }
      })
    }
  },

  /**切换基本信息、高级信息、印象
   * 
   */
  qietap: function (param) {
    if (param.currentTarget.id == 1) {
      this.data.open = 1;
      let obj = new Object({
        detail: {
          current: 0
        }
      });
      this._bindchange(obj);
      var current_num = 0;
      //调用之前关闭
      clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
      //开启定时器 铺获
      this._MonitorPage('#rich_one');
    } else if (param.currentTarget.id == 2) {
      this.data.open = 2;
      let obj = new Object({
        detail: {
          current: 1
        }
      });
      this._bindchange(obj);
      var current_num = 1;
      //调用之前关闭
      clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
      //开启定时器 铺获
      this._MonitorPage('#rich_two');
    } else if (param.currentTarget.id == 3) {
      this.data.open = 3;
      let obj = new Object({
        detail: {
          current: 2
        }
      });
      this._bindchange(obj);
      var current_num = 2;
    }
    this.setData({
      open: this.data.open,
      _current: current_num
    });
  },
  /**
   * 
   * @param {滑块切换按钮} res 
   */
  _bindchange: function (res) {
    if (res.detail.current == 0) {
      this.rich = '#rich_one';
      //调用之前关闭
      clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
      //开启定时器 铺获
      this._MonitorPage('#rich_one');
    } else if (res.detail.current == 1) {
      this.rich = '#rich_two';
      //调用之前关闭
      clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
      //开启定时器 铺获
      this._MonitorPage('#rich_two');
    } else if (res.detail.current == 2) {
      this.rich = '#rich_three';
      clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
      this._MonitorPage('#rich_three');
      // this.data.opens = 3;
    }

    this.setData({
      open: (res.detail.current + 1),
      _current: res.detail.current
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //调用之前关闭
    clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
    //开启定时器 铺获
    this._MonitorPage('#rich_one');
  },

  /**
   * 
   * @param {跳转选项列表} param 
   */
  GoToChooseList: function (event) {
    //根属性id
    let root_id = event.currentTarget.dataset.rootid;
    //索引下标
    let index = event.currentTarget.dataset.index;
    //选项类型 1是单选 2是多选
    let type = event.currentTarget.dataset.listtype;
    //判断是否是地址选项列表
    // let area = ['定居地'];
    let areaType = ['定居地'].indexOf(this.data.basic_list[index].att_name);
    //跳转选项地址
    var url = '/pages/zhaon_ziown/zhaon_ziown_bianji/lucy_jibenxuanze/lucy_jibenxuanze';


    let basic = {
      //1是普通选项，2是地址选项
      'choose_type': areaType != '-1' ? '2' : '1',
      //某一列索引
      'index': index,
      //1是单选列表，2是多选列表
      'list_type': type,
      //某一列数据
      'arr_data': this.data.basic_list[index],
      
    };
    wx.navigateTo({
      url: url,
      events: {
        back: (res) => {
          this.data.basic_list[res.index] = res.arr_data;
          this.setData({
            basic_list: this.data.basic_list
          });
          console.log(this.data.basic_list);
        }
      },
      success: (res) => {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('basic', basic);
      }
    });
  },

  /**
   * 跳转选择页
   */
  toxuanze2: function (res) {
    var that = this;
    console.log(res.target.dataset);
    var cname = res.target.dataset.cname;
    var columid = res.target.dataset.columid;
    var listtype = res.target.dataset.listtype;
    var opttype = res.target.dataset.opttype;

    wx.navigateTo({
      url: '/pages/zhaon_xuanzhe/zhaon_xuanzhe2?cname=' + cname + '&columid=' + columid + '&listtype=' + listtype + '&opttype=' + opttype,
      // url: '/pages/zhaon_xuanzhe2/zhaon_xuanzhe2',
    });

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // const eventChannel = this.getOpenerEventChannel();
    // eventChannel.on('account', (data) => {
    //   console.log(data)
    //   this.account_info = data;
    // });
    // let that = this;


    /*
      高级信息
    */
    wx.request({
      url: 'https://zhiyu.zyfuns.com/api/Zhaon_Get_Height/GetHeightInfo',
      method: 'POST',
      data: {
        openid: app.globalData.openid,
      },
      success: (res) => {
        // console.log(res.data.data)
        this.setData({
          advanced_list: res.data.data
        })
      },
    })

    // var that = this;
    // this.onLoad();
    // wx.getStorage({
    //   key: 'basic_list',
    //   success(res) {
    //     console.log(res);
    //     that.setData({
    //       basic_list: res.data
    //     })
    //   }
    // })
  },
  /**
   * 昵称修改监听
   */
  emailInput: function (e) {
    var user_nicheng = e.detail.value;
    console.log(user_nicheng);
    this.setData({
      user_nicheng: user_nicheng
    });
  },
  /**
   * 保存按钮
   */
  save_btn: function () {
    wx.reLaunch({
      url: '/pages/zhaon_ziown/zhaon_ziown',
    });
  },
  /**
   * 
   * @param {
   *监听 页面长度
   *page : 监听页面id元素
   *LogSetInterval: 初始化巡航铺获
   *LogSetIntNum: 记录巡航铺获15次退出
   *}
   */
  _MonitorPage: function (page, LogSetIntNum = 15, LogSetInterval = new Array()) {
    this.SetIntNumberUserInfo_One = setInterval(function (param) { //巡航铺获
      wx.createSelectorQuery().select(page).boundingClientRect(function (res) {
        if (res == null) {
          clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
          return;
        }
        let heightView = res.height + 300;
        if (app.globalData.windowHeight >= heightView) {
          this.setData({
            height: (app.globalData.windowHeight - app.globalData.statusBarHeight * 2 - 70) + 'px',
          });
        } else {
          this.setData({
            height: (res.height + 100) + 'px',
          });
        }
        LogSetInterval.push(1);
        if (LogSetInterval.length == LogSetIntNum) {
          clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
        }
      }.bind(this)).exec();
    }.bind(this), 98);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})